package com.cool4j.generator.builder

import com.cool4j.generator.entity.Table
import com.cool4j.generator.exception.ExceptionMessage
import com.cool4j.generator.util.GeneratorUtil
import com.cool4j.generator.util.LogUtil
import com.cool4j.view.entity.HandlerVO
import freemarker.template.Configuration
import freemarker.template.TemplateException

/**
 * 编辑页面构造器
 *
 * @author Frank create on 16/1/8 15:29
 */
class EditPageBuilder extends BaseBuilder {

    @Override
    boolean builder(Table table, String packagePath, HandlerVO handlerVO, Configuration config) {
        try {
            /* 获取或创建一个模板 */
            def template = config.getTemplate("EditPage.ftl", "UTF-8")
            def dataMap = getDataMap(table, packagePath, handlerVO)
            def filePath = handlerVO.getOutputPath() + "/view/" + table.getClassName() + "Edit.ftl"
            GeneratorUtil.writeFile(filePath, template, dataMap)
        } catch (IOException e) {
            LogUtil.logging(ExceptionMessage.LOG_EDIT_PAGE_BUILDER_IO_EXCEPTION, e)
            return false
        } catch (TemplateException e) {
            LogUtil.logging(ExceptionMessage.LOG_EDIT_PAGE_BUILDER_TEMPLATE_EXCEPTION, e)
            return false
        } catch (Exception e) {
            LogUtil.logging(ExceptionMessage.LOG_EDIT_PAGE_BUILDER_OTHER_EXCEPTION, e)
            return false
        }
        return true
    }
}
